let websocket: any, lockReconnect = false;
websocket = new WebSocket('ws://localhost:10000/basic');
let createWebSocket = (url: any) => {

    // websocket.onopen = function () {
    //     heartCheck.reset().start();
    // }
    websocket.onerror = function () {
        reconnect(url);
    };
    websocket.onclose = function (e: any) {
        console.log('websocket 断开: ' + e.code + ' ' + e.reason + ' ' + e.wasClean)
    }

}
let reconnect = (url: any) => {
    if (lockReconnect) return;
    //没连接上会一直重连，设置延迟避免请求过多
    setTimeout(function () {
        createWebSocket(url);
        lockReconnect = false;
    }, 4000);
}
function sendmsg(msg: any) {
    websocket.send(msg);

}

//关闭连接
let closeWebSocket = () => {
    websocket && websocket.close();
}
export {
    websocket,
    createWebSocket,
    closeWebSocket,
    sendmsg
};

